//
//  ALPPlayButton.m
//  AlpFM
//
//  Created by KING on 16/2/26.
//  Copyright © 2016年 Alp. All rights reserved.
//

#import "ALPPlayButton.h"
#import "ALPPlayViewController.h"
@implementation ALPPlayButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews{
    
    self.backgroundColor = [UIColor clearColor];
    self.frame = CGRectMake(SCREEN_WIDTH * 0.4, -SCREEN_WIDTH *0.07, SCREEN_WIDTH *0.2, SCREEN_HEIGHT *0.2);
    self.userInteractionEnabled = YES;
    
    //背景
    self.btnBackground = [[UIImageView alloc] initWithFrame:self.bounds];
    self.btnBackground.backgroundColor = [UIColor whiteColor];
    //切掉圆角边
    self.btnBackground.layer.masksToBounds = YES;
    self.btnBackground.layer.cornerRadius = SCREEN_WIDTH *0.1;
    self.btnBackground.tag = 1000;
    [self addSubview:self.btnBackground];
    
    //圆圈
    UIImageView *imageC = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.2, SCREEN_WIDTH *0.2)];
    imageC.image = [UIImage imageNamed:@"tabbar_np_loop"];
    imageC.backgroundColor = [UIColor clearColor];
    [self.btnBackground addSubview:imageC];
    //在btnBackground 这个小框框里的坐标
    imageC.center = CGPointMake(SCREEN_WIDTH*0.1, SCREEN_WIDTH*0.1);
    
    //旋转ImageView
    self.musicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.musicBtn.backgroundColor = [UIColor clearColor];
    self.musicBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH * 0.15, SCREEN_WIDTH * 0.15);
    self.musicBtn.center = CGPointMake(SCREEN_WIDTH * 0.1, SCREEN_WIDTH * 0.1);
    //切掉圆角边
    self.musicBtn.layer.masksToBounds = YES;
    self.musicBtn.layer.cornerRadius = SCREEN_WIDTH * 0.15 / 2;
    //以后会改
    [self.musicBtn setBackgroundImage:[UIImage imageNamed:@"girledg"] forState:UIControlStateNormal];
    [imageC addSubview:self.musicBtn];
    
    
    //播放按钮
    self.playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playBtn.backgroundColor = [UIColor clearColor];
    self.playBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH * 0.15, SCREEN_WIDTH * 0.15);
    self.playBtn.center = CGPointMake(SCREEN_WIDTH * 0.1, SCREEN_WIDTH * 0.1);
    //切掉圆角边
    self.playBtn.layer.masksToBounds = YES;
    self.playBtn.layer.cornerRadius = SCREEN_WIDTH * 0.15 / 2;
    //以后会改
    [self.playBtn setBackgroundImage:[UIImage imageNamed:@"toolbar_play_n_p"] forState:UIControlStateNormal];
    self.playBtn.alpha = 0.6;
    [self.playBtn addTarget:self action:@selector(playMusic:) forControlEvents:UIControlEventTouchUpInside];
    [imageC addSubview:self.playBtn];
    //开启交互
    self.btnBackground.userInteractionEnabled = YES;
    imageC.userInteractionEnabled = YES;
    
    
}

//playBtn会转
- (void)playMusic:(UIButton *)btn{
    [self startCircle];
    
    ALPPlayViewController *player = [ALPPlayViewController sharePlayerViewController];
//    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
    //播放上次存储的记录
//    NSString *arrayPath = [DOCUMENT stringByAppendingPathComponent:@"musicListData.txt"];
//    NSString *str = [[NSString alloc] initWithContentsOfFile:arrayPath encoding:NSUTF8StringEncoding error:nil];
    
    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"str"];
    
    player.song_ID = str.intValue;
    
    [player.player resume];
    
    [self.window.rootViewController presentViewController:player animated:YES completion:^{
        
    }];
    
}

#pragma mark - 开始旋转
- (void)startCircle{
    //改变z轴
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //旋转起始值 0
    animation.fromValue = [NSNumber numberWithInt:0];
    //最终旋转的角度2π
    animation.toValue = [NSNumber numberWithInt:M_PI * 2];
    //旋转时长
    animation.duration = 5;
    //重复次数
    animation.repeatCount = NSIntegerMax;  //哪来的
    //旋转结束后是否要旋转回来
    animation.autoreverses = NO;
    //是否按照结束位子继续旋转
    animation.cumulative = YES;
    
    //给view的layer层添加动画,key是标记  随便起名字
    [self.musicBtn.layer addAnimation:animation forKey:@"comeOn"];
    self.musicBtn.layer.speed = 1.0;
    //隐藏播放按钮
    self.playBtn.alpha = 0;
    self.playBtn.enabled = NO;
    
}

#pragma mark - 停止播放
-(void)stopCircle
{
    self.musicBtn.layer.speed = 0;
    self.playBtn.alpha = 1;
    self.playBtn.enabled = YES;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
